#!/bin/bash
WOEK_NOW=$(dirname $(readlink -f "$0"))"/"
source $WOEK_NOW/config

case $(uname -m) in
    i386)   title="ubuntu:"$(ifconfig ens33 |grep "ether" |awk '{print  $2}');;
    x86_64) title="ubuntu:"$(ifconfig ens33 |grep "ether" |awk '{print  $2}');;
    *)      title="imx:"$(cat /sys/fsl_otp/HW_OCOTP_CFG0)$(cat /sys/fsl_otp/HW_OCOTP_CFG1);;
esac

show(){
  echo $title
}

start() {
    i=0; loop=60; sleepTime=60 # 60*60=1h 退出
    while [ $i -le $loop ]; do
        let i++
        remote_ip="www.baidu.com"
        ping -c 2  -w 5  $remote_ip > /dev/null
        if [ $? -eq 0 ] ;then
              npc -server=$npcport -vkey=$title -type=tcp   > /dev/null 2>&1 &
              sleep 3600
              return 0
        else
                sleep $sleepTime
        fi
    done
    return 1
}

stop() {
  killall npc > /dev/null 2>&1
}

case "$1" in
  show)
    show
    ;;
  start)
    echo  "Starting connect to nps for 1 hour"
    stop
    start
    stop
    ;;
  stop)
    stop
    ;;
  restart|reload)
    stop
    start 
	;;
  *)
	  echo "Usage: $0 {start|stop|restart|show}"
	  exit 1
esac

exit $?